<template>
  <hi-row
    isColumn
    :wrap="false"
    class="viewcontainer"
    v-if="dataset && subDataSet"
  >
    <hi-col flex="300px">
      <hi-toolbar
        ref="dbtoolbar"
        :calloutcofig="calloutcofig"
        :source="dataset"
        v-bind="toolbar"
        :addOne="true"
        :disabled="dataset.isReadonly()"
      ></hi-toolbar>
      <btn-toolbar @callFunc="clickBtn" class="btn-toolbar"></btn-toolbar>
      <print
        :form-data="dataset.data[0]"
        :table-data="subDataSet.data"
        ref="print"
        v-if="dataset.data[0] && subDataSet.data"
      ></print>
      <hi-form
        ref="detailForm"
        :dataSource="dataset"
        :model="dataset.curRecord"
        v-bind="form"
        :disabled="dataset.isReadonly()"
      ></hi-form>
    </hi-col>
    <hi-col flex="auto" :min-height="200">
      <hi-table
        ref="detailTable"
        :dataSource="subDataSet"
        v-bind="dataTable"
        :model="subDataSet.data"
        :isEdit="!subDataSet.isReadonly()"
      ></hi-table>
    </hi-col>
    <el-dialog
      title="电子监管码"
      :visible.sync="supervisionCodeDialog"
      width="60%"
      :close-on-click-modal="false"
    >
      <outCode
        :dataset="codeList"
        :auditData="dataset"
        :codeData="codeData"
        :pmodel="pmodel"
        v-if="supervisionCodeDialog"
      ></outCode>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="supervisionCodeDialog = false"
          >关 闭</el-button
        >
        <!-- <el-button type="primary">确 定</el-button> -->
      </span>
    </el-dialog>
  </hi-row>
</template>

<script>
import Print from "./print";
import BtnToolbar from "@/views/component/toolbar/index.vue";
import { HiForm, DataSetContainer, HiToolbar, HiNumber, HiTable } from "hi-ui";
import viewBase from "../../base";
import { cloneDeep } from "lodash";
import toolbar from "@/hiuiDefaultConfig/toolbar";
import storeDealwithConfig from "./dataset/dealwithlist";
import storeDealwithDetailConfig from "./dataset/detail";
import audittag from "@/views/component/audittag";
import takeStockConfig from "../takestock/dataset/takestocklist";
import outCode from "@/views/supervisioncode/outCode";
import supervisionCodeCfg from "@/views/supervisioncode/outCode/dataset";
export default {
  name: "dealwithdetail",
  extends: viewBase,
  components: {
    HiForm,
    HiToolbar,
    HiTable,
    audittag,
    HiNumber,
    Print,
    BtnToolbar,
    outCode,
  },

  data() {
    return {
      supervisionCodeDialog: false,
      massage: null,
      calloutcofig: {
        param: {
          FAUDIT: 0,
        },
        storeCfg: takeStockConfig,
        active: true,
        /* treeOption:{
                            "modelFile":"users",
                            label: "name",
                            children: "company"
                        }, */

        multiSelect: false,
        allOrderReturn: false,
        columns: [
          {
            name: "FNUMBER",
            label: "单号",
            width: 140,
          },
          {
            name: "FSHOPDESC",
            label: "店铺",
            width: 140,
          },
          {
            name: "FUSERID",
            label: "用户",
          },
          {
            name: "FCREATEDATE",
            label: "时间",
          },
        ],
        queryOption: {
          fields: [
            {
              name: "FNUMBER",
              element: "el-input",
              label: "单号",
            },
            {
              name: "FSHOPDESC",
              element: "el-input",
              label: "店铺",
            },
          ],
        },
        details: [
          {
            target: {
              id: "DealwithEntity",
            }, //目标
            source: {
              id: "TakestockEntity",
              modelFile: "store/takestock/dataset/TakestockEntity.xml",
            }, //来源
            // queryCdions: {}, //来源数据集查询条件
            updateFields: [], //更新字段
            limitEqualsFields: [], //相同字段
            uniqueFields: [], //主键
            returnFields: {
              FTACKNUMBER: "FNUMBER",
              FSHOPDESC: "FSHOPDESC",
              FSHOPDESCID: "FSHOPDESCID",
            }, //返回字段
          },
          {
            target: {
              id: "DealwithDetailEntity",
            }, //目标
            source: {
              id: "TakestockDetailEntity",
              modelFile: "store/takestock/dataset/TakestockDetailEntity.xml",
            }, //来源
            queryCdions: {
              FNUMBER: function (rec) {
                return rec["FTACKNUMBER"];
              },
            }, //来源数据集查询条件
            updateFields: [], //更新字段
            limitEqualsFields: [], //相同字段
            uniqueFields: [], //主键
            returnFields: {
              FGOODSNUMBER: "FGOODSNUMBER",
              FBARCODE: "FBARCODE",
              FDESC: "FDESC",
              FBRAND: "FBRAND",
              FUNIT: "FUNIT",
              FPRICE: "FPRICE",
              FSTOCKAMOUNT: "FSTOCKAMOUNT",
              FINVAMOUNT: "FINVAMOUNT",
              FPLAMOUNT: "FPLAMOUNT",
              FPLMONEY: "FPLMONEY",
              FSPEC: "FSPEC",
            }, //返回字段
          },
        ],
      },

      //数据集
      dataset: null,
      //数据集配置
      datasetCfg: storeDealwithConfig,
      //从表数据集
      subDataSet: null,
      //从表数据集配置
      subDatasetCfg: storeDealwithDetailConfig,
      //从从表的数据集
      thridDataSet: null,
      //从从表的数据集配置
      thridDataSetCfg: supervisionCodeCfg,
      //数据桥梁
      dataBridge: new Map(),
      //数据表格
      dataTable: {
        isEdit: true,
        hasOperAdd: false,
        hasSelection: true,
        hasRowNumber: true,
        showSummary: true,
        columns: [
          {
            name: "FGOODSNUMBER",
          },
          {
            label: "电子监管码",
            width: 120,
            slot: [
              {
                element: "el-button",
                label: "电子监管码",
                props: {
                  size: "mini",
                  round: true,
                },
                handles: {
                  click: this.opencode,
                },
              },
            ],
          },
          {
            name: "FBARCODE",
          },

          {
            name: "FDESC",
          },
          {
            name: "FBRAND",
          },
          {
            name: "FSPEC",
          },
          {
            name: "FUNIT",
          },
          {
            name: "FSTOCKAMOUNT",
            props: {
              precision: 2,
            },
          },

          {
            name: "FINVAMOUNT",
            props: {
              precision: 2,
            },
          },
          {
            name: "FPLAMOUNT",
            props: {
              precision: 2,
            },
          },
          {
            name: "FPRICE",
            props: {
              format: "￥0.00",
              precision: 2,
            },
          },
          {
            name: "FPLMONEY",
            props: {
              format: "￥0.00",
              precision: 2,
            },
          },
          /*  {
            name: "FUSERID",
          },
          {
            name: "FCREATEDATE",
          }, */
          {
            name: "FNOTE",
          },
        ],
      },
      toolbar: [],
      //表单配置
      form: {
        columnsTotal: 2,
        items: [
          {
            name: "FNUMBER",
          },
          {
            name: "FTACKNUMBER",
          },
          {
            name: "FNOTE",
            colspan: 2,
            props: {
              type: "textarea",
              autosize: { minRows: 4, maxRows: 4 },
              maxlength: 200,
            },
          },
          {
            name: "FUSERID",
          },
          {
            name: "FCREATEDATE",
          },
          {
            name: "FSHOPDESC",
          },
          {
            element: audittag,
            name: "FAUDIT",
          },
        ],
      },
    };
  },
  model: {
    prop: "value",
    event: "valuechange",
  },
  props: {
    value: {
      type: Object,
    },
  },

  computed: {},
  watch: {},
  methods: {
    clickBtn(methodName) {
      switch (methodName) {
        case "print":
          this.$refs.print.onPrint();
          break;
      }
    },
    opencode(evt, data, grid) {
      debugger;
      this.pmodel = grid.dataSource.getModel(data);
      this.codeData = data;
      this.codeList = grid.dataSource
        .getModel(data)
        .getChildStore("SupervisioncodeEntity_out");
      this.supervisionCodeDialog = true;
    },
    addDetail() {
      debugger;
      this.subDataSet.add({});
    },
  },
  created() {
    this.toolbar = cloneDeep(toolbar);
    this.toolbar.buttons.push({
      kind: "callout",
    });
  },
  mounted() {
    //创建主数据集
    this.dataset = this.createDataSet(this.datasetCfg);
    this.dataset.on("dirtychange", (store, dirty) => {
      console.log("dataset" + dirty);
    });
    this.dataset.on("beforesave", (store) => {
      let shopDesc = store.data[0].FSHOPDESC,
        shopId = store.data[0].FSHOPDESCID;
      let subStore = store.models[0].getChildStore("DealwithDetailEntity"),
        len = subStore.data.length;
      for (let i = 0; i < len; i++) {
        let item = subStore.data[i];
        item.FSHOPDESC = shopDesc;
        item.FSHOPDESCID = shopId;
      }
    });
    //创建从数据集
    this.subDataSet = this.createDataSet(this.subDatasetCfg, this.dataset);
    //创建从从表数据集
    this.thridDataSet = this.createDataSet(
      this.thridDataSetCfg,
      this.subDataSet
    );
    //设置数据与组件的关系
    this.dataBridge.set(this.dataset, ["detailForm", "detailTable"]);
    this.flowConfig = { dataset: this.dataset };
    if (this.$route.query.id) {
      this.dataset.load({
        FID: this.$route.query.id,
      });
    }
  },
};
</script>
<style rel="stylesheet/scss" lang="scss" scope>
</style>

